package com.ifatter.tick;

import com.ifatter.asyn.RegiAsyn;
import com.ifatter.model.RegiModel;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegistActivity extends BaseActivity {
	String user_name;
	String pass_word;
	String re_pass;
	private EditText username;
	private EditText password;
	private EditText repass;

	private TextView tip_un;
	private TextView tip_pw;
	private TextView tip_rpw;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_regist);

		findViewById(R.id.btn_left).setVisibility(View.GONE);
		((TextView) findViewById(R.id.title)).setText("用户注册");
		findViewById(R.id.btn_right).setVisibility(View.GONE);

		username = (EditText) findViewById(R.id.edit_username);
		password = (EditText) findViewById(R.id.edit_password);
		repass = (EditText) findViewById(R.id.edit_repass);

		tip_un = (TextView) findViewById(R.id.tip_username);
		tip_pw = (TextView) findViewById(R.id.tip_password);
		tip_rpw = (TextView) findViewById(R.id.tip_repass);

		username.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					tip_un.setVisibility(View.INVISIBLE);
				} else {
					validateUsername();
				}
			}
		});

		password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					tip_pw.setVisibility(View.INVISIBLE);
				} else {
					validatePassword();
				}
			}
		});

		repass.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					tip_rpw.setVisibility(View.INVISIBLE);
				} else {
					validateRePass();
				}
			}
		});
		repass.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				validateRePass();
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void afterTextChanged(Editable s) {
			}
		});
	}

	private boolean validateUsername() {
		String un = username.getText().toString();
		if (TextUtils.isEmpty(un)) {
			tip_un.setText("❌请输入用户名");
			tip_un.setVisibility(View.VISIBLE);
		} else {
			int length = un.length();
			if (length < 6 || length > 12) {
				tip_un.setText("❌必须是6～12个英文");
				tip_un.setVisibility(View.VISIBLE);
			} else if (!un.matches("[a-zA-Z0-9_]+")) {
				tip_un.setText("❌必须英文或_");
				tip_un.setVisibility(View.VISIBLE);
			} else {
				return true;
			}
		}
		return false;
	}

	private boolean validatePassword() {
		String un = password.getText().toString();
		if (TextUtils.isEmpty(un)) {
			tip_pw.setText("❌请输入密码");
			tip_pw.setVisibility(View.VISIBLE);
		} else {
			int length = un.length();
			if (length < 6 || length > 12) {
				tip_pw.setText("❌必须是6～12个英文");
				tip_pw.setVisibility(View.VISIBLE);
			} else {
				return true;
			}
		}
		return false;
	}

	private boolean validateRePass() {
		if (validatePassword()) {
			String un = repass.getText().toString();
			String pass = password.getText().toString();
			if (TextUtils.equals(un, pass)) {
				tip_rpw.setVisibility(View.INVISIBLE);
				return true;
			} else {
				tip_rpw.setText("❌两次密码不一致");
				tip_rpw.setVisibility(View.VISIBLE);
			}
		}
		return false;
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.regist: {
			if (validateUsername() && validatePassword() && validateRePass()) {
				Toast.makeText(this, "regist", Toast.LENGTH_SHORT).show();
				user_name=username.getText().toString();
				pass_word=password.getText().toString();
				re_pass=repass.getText().toString();
			    new RegiAsyn(user_name,pass_word,re_pass){

					@Override
					protected void onPostExecute(Object result) {
						// TODO Auto-generated method stub
						RegiModel regiModel=(RegiModel) result;
						System.out.println("----"+regiModel.user);
					}
					
				}.execute();
			}
			
			
			break;
		}
		case R.id.login: {
			startActivity(new Intent(this, LoginActivity.class));
			break;
		}
		}
	}
}
